﻿using SharpSoft.WPF;
using SharpSoft.WPF.Adorners;
using SharpSoft.WPF.Masks;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Media;

namespace System
{
    public static class Extensions
    {
        #region Mask

        /// <summary>
        /// 创建并立即打开一个遮罩层。
        /// </summary>
        /// <param name="visual"></param>
        /// <param name="content">要显示的提示内容</param>
        /// <param name="timeout">设置显示时长，为null则会一直显示，直到调用<see cref="SharpSoft.WPF.Busy"/>的<see cref="SharpSoft.WPF.Busy.Close"/>方法。</param>
        /// <returns></returns>
        public static async Task<Mask> Mask(this UIElement visual, object content, TimeSpan? timeout = null)
        {
            AdornerLayer adornerLayer = AdornerLayer.GetAdornerLayer(visual);
            while (adornerLayer == null)
            {
                visual = VisualTreeHelper.GetChild(visual, 0) as UIElement;
                adornerLayer = AdornerLayer.GetAdornerLayer(visual);
                if (visual == null)
                {
                    break;
                }
            }
            if (visual == null)
            {
                throw new Exception("不能在此UI上创建装饰器。");
            }
            var ml = new MaskLayout();

            ml.MaskContent = content;
            var VA = new VisualAdorner(visual) { ContentVisual = ml };
            var b = new Mask(VA, adornerLayer);
            b.Open();
            if (timeout.HasValue)
            {
                await Task.Delay(timeout.Value);
                b.Close();
            }
            return b;
        }
        #endregion


        #region Toast
        /// <summary>
        /// 显示一条Toast消息
        /// </summary>
        /// <param name="visual"></param>
        /// <param name="title"></param>
        /// <param name="msg"></param>
        /// <param name="type"></param>
        /// <param name="timeout"></param>
        /// <returns></returns>
        public async static Task<Toast> Toast(this UIElement visual, string title, string msg, ToastTemplate.ToastType type = ToastTemplate.ToastType.Info, TimeSpan? timeout = null)
        {
            AdornerLayer adornerLayer = AdornerLayer.GetAdornerLayer(visual);
            while (adornerLayer == null)
            {
                visual = VisualTreeHelper.GetChild(visual, 0) as UIElement;
                adornerLayer = AdornerLayer.GetAdornerLayer(visual);
                if (visual == null)
                {
                    break;
                }
            }
            if (visual == null)
            {
                throw new Exception("不能在此UI上创建装饰器。");
            }
            var ml = new MaskLayout();
            ToastTemplate toastTemplate = new ToastTemplate()
            {
                Title = title,
                Message = msg,
                Type = type,
                HorizontalAlignment = HorizontalAlignment.Stretch,
                VerticalAlignment = VerticalAlignment.Stretch
            };
            ml.MaskContent = toastTemplate;
            var VA = new VisualAdorner(visual) { ContentVisual = ml };
            var t = new Toast(VA, adornerLayer);
            t.Open();
            if (timeout.HasValue)
            {
                await Task.Delay(timeout.Value);
                t.Close();
            }
            return t;
        }


        #endregion

    }
}
